package com.coincalf.wallet.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.coincalf.framework.dto.CoinWithdrawDTO;
import com.coincalf.framework.dto.WithdrawResultDTO;
import com.coincalf.framework.http.Response;
import com.coincalf.wallet.blockchain.bean.TransInfo;
import com.coincalf.wallet.dto.CoinWithdrawDTOMapper;
import com.coincalf.wallet.entity.CoinWithdraw;
import com.coincalf.wallet.service.CoinWithdrawService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 数字货币提现 Controller
 * </p>
 *
 * @author crow
 * @since 2018-05-16
 */
@Slf4j
@RestController
@RequestMapping("/withdraw")
@Api(value = "数字货币提现", description = "数字货币提现")
public class CoinWithdrawController {

    @Autowired
    private CoinWithdrawService coinWithdrawService;
    @Autowired
    private CommonController commonController;

    /**
     * 查询用户提现记录
     *
     * @param page
     * @param coinId
     * @return
     */
    //@PreAuthorize("isAuthenticated()")
    @GetMapping(value = "/record")
    @ApiOperation(value = "用户提现", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页码", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页大小", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "coinId", value = "币种ID", dataType = "long", paramType = "query")
    })
    public Response queryWithdrawRecord(HttpServletRequest request,
                                        @ApiIgnore Page<CoinWithdraw> page,
                                        @RequestParam("coinId") long coinId) {
        Long userId = commonController.getUserId(request);
        EntityWrapper<CoinWithdraw> e = new EntityWrapper<>();
        e.eq("user_id", userId);
        if (coinId != 0L) {
            e.eq("coin_id", coinId);
        }
        e.orderBy("created", false);
        Page<CoinWithdraw> coinWithdrawPage = coinWithdrawService.selectPage(page, e);
        return Response.ok(coinWithdrawPage);
    }

    /**
     * 申请提币
     *
     * @return
     */
    @PostMapping
    public Long applyWithdraw(HttpServletRequest request, @RequestBody CoinWithdrawDTO coinWithdrawDTO) {
        //Long userId = commonController.getUserId(request);
        CoinWithdraw coinWithdraw = CoinWithdrawDTOMapper.INSTANCE.from(coinWithdrawDTO);
        //coinWithdraw.setUserId(userId);
        return coinWithdrawService.applyWithdraw(coinWithdraw);
    }

    /**
     * 已打币，待扣减账户资金
     *
     * @return
     */
    @GetMapping("/pending")
    public List<CoinWithdrawDTO> queryPendingRecord() {
        return coinWithdrawService.queryPendingRecord();
    }

    /**
     * 打币中...
     *
     * @return
     */
    @GetMapping("/sendCoining/{coinName}")
    public List<CoinWithdrawDTO> querySendCoiningByCoinName(@PathVariable("coinName") String coinName) {
        return coinWithdrawService.querySendCoiningByCoinName(coinName);
    }

    /**
     * 已审核通过，未打币
     *
     * @return
     */
    @GetMapping("/pending/{coinName}")
    public List<CoinWithdrawDTO> queryPassedRecordByCoinName(@PathVariable("coinName") String coinName) {
        return coinWithdrawService.queryPassedRecordByCoinName(coinName);
    }

    /**
     * 提币打币成功
     *
     * @param coinWithdraw 充值记录
     * @return
     */
    @PutMapping("/success")
    public boolean modifyWithdrawStatus(@RequestBody CoinWithdrawDTO coinWithdraw) {
        return coinWithdrawService.modifyWithdrawStatus(coinWithdraw.getId(), coinWithdraw.getStatus());
    }

    //转账
    @PostMapping("/sendCoin")
    public TransInfo sendCoin(@RequestBody TransInfo transInfo) {
        return coinWithdrawService.sendCoin(transInfo);
    }

    //提币链上转账后修改交易状态
    @PostMapping("/updateTxStatus")
    public boolean updateTxStatus(@RequestBody WithdrawResultDTO withdrawResultDTO) {
        synchronized (CoinWithdrawController.class) {
            return coinWithdrawService.updateTxStatus(withdrawResultDTO);
        }
    }

}

